<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/16
 * Time: 20:08
 */

namespace app\api\controller;


use app\api\model\User as UserModel;
use think\Db;

class Bonus
{
    //每周权重分红记录
    public function weekBonusList($page = 1, $size = 10)
    {
        $list = Db::name('week_bonus')->page($page, $size)->select();
        return json(['errorCode' => 0, 'msg' => '每周分红记录', 'data' => $list]);
    }

    //权重排行
    public function weightRanking($page = 1, $size = 10)
    {
        $list = UserModel::where('bonus_weight', '>', 0)->order('bonus_weight desc')->page($page, $size)->select();
        return json(['errorCode' => 0, 'msg' => '权重排行', 'data' => $list]);
    }

    //分红池总金额
    public function bonusPool()
    {
        $param = Db::name('config_param')->where('key', ['=', 'pool_base'], ['=', 'weight_base'], ['=', 'bonus_pool'], 'or')->column('value','key');
        $total_weight = UserModel::where('bonus_weight', '>', 0)->sum('bonus_weight');
        $each_weight = 0.00;
        /*$start = date('Y-m-d', strtotime('last Wednesday'));
        $end = date('Y-m-d', strtotime('Tuesday'));*/
        $w = date('w');
        if ($w == 3){
            $start = date('Y-m-d');
            $end = date('Y-m-d', strtotime('Tuesday'));
        }else{
            $start = date('Y-m-d', strtotime('last Wednesday'));
            $end = date('Y-m-d', strtotime('Tuesday'));
        }
        $title = $start . '至' . $end;
        $amount = $param['bonus_pool'] + $param['pool_base'];
        $total_weight = $total_weight + $param['weight_base'];
        if ($total_weight > 0) {
            $each_weight = floor(($amount * 100 / $total_weight)) / 100;
        }
        $data = [
            'title' => $title,
            'bonus_pool' => $amount,
            'total_weight' => $total_weight,
            'each_weight' => $each_weight,
        ];
        return json(['errorCode' => 0, 'msg' => '分红池总金额', 'data' => $data]);
    }
}